package interfaces;
import com.toedter.calendar.JDateChooser;

import dominio.Alquilerbicicleta;
import dominio.AlquilerbicicletaId;
import dominio.Alquilermaterialdeportivo;
import dominio.AlquilermaterialdeportivoId;
import dominio.Bicicleta;
import dominio.Materialdeportivo;
import dominio.Socio;
import dominio.Usuario;
import excepciones.DAOExcepcion;
import gestion.AlquileresBicicleta;
import gestion.AlquileresMaterialDeportivo;
import gestion.Bicicletas;
import gestion.MaterialesDeportivos;
import gestion.Socios;
import gestion.Usuarios;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaAlquilerBicicleta extends javax.swing.JDialog {
	private JLabel jLabelEsnCard;
	private JLabel jLabelFechaAlquiler;
	private JTextField jTextFieldOtro;
	private JLabel jLabelPago;
	private JTextField jTextFieldPago;
	private JComboBox jComboBoxTipoPago;
	private JLabel jLabelTipoPago;
	private JTextArea jTextAreaEstadoEntrega;
	private JLabel jLabelEstadoEntrega;
	private JComboBox jComboBoxCasco;
	private JTextField jTextFieldIdBicicleta;
	private JComboBox jComboBoxChaleco;
	private JComboBox jComboBoxCandado;
	private JButton jButtonGuardar;
	private JButton jButtonCancelar;
	private JDateChooser jDateChooserFechaDevuelto;
	private JDateChooser jDateChooserFechaDevolucion;
	private JDateChooser jDateChooserFechaAlquiler;
	private JLabel jLabelOtro;
	private JLabel jLabelChaleco;
	private JLabel jLabelCandado;
	private JLabel jLabelCasco;
	private JTextField jTextFieldEsnCard;
	private JLabel jLabelFechaDevuelto;
	private JLabel jLabelFechaDevolucion;
	private JLabel jLabelIdBicicleta;

	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				VentanaAlquilerBicicleta inst = new VentanaAlquilerBicicleta(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public VentanaAlquilerBicicleta(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	public VentanaAlquilerBicicleta(/*String email*/) {
		super();
		//TODO this.emailConnectado = email;
		initGUI();
	}
	
	
	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.columnWidths = new int[] {130, 10, 240};
				thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0};
				thisLayout.rowHeights = new int[] {0, -40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 40, 0, 0, 0};
				thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
				getContentPane().setLayout(thisLayout);
				this.setTitle("ESN Office - Alquiler bicicleta");
				{
					jLabelEsnCard = new JLabel();
					getContentPane().add(jLabelEsnCard, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelEsnCard.setText("ESN Card *");
				}
				{
					jLabelIdBicicleta = new JLabel();
					getContentPane().add(jLabelIdBicicleta, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelIdBicicleta.setText("ID Bicicleta *");
				}
				{
					jLabelFechaAlquiler = new JLabel();
					getContentPane().add(jLabelFechaAlquiler, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaAlquiler.setText("Fecha de alquiler *");
				}
				{
					jLabelFechaDevolucion = new JLabel();
					getContentPane().add(jLabelFechaDevolucion, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaDevolucion.setText("Fecha de devolución *");
				}
				{
					jLabelFechaDevuelto = new JLabel();
					getContentPane().add(jLabelFechaDevuelto, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaDevuelto.setText("Fecha devuelta");
				}
				{
					jTextFieldEsnCard = new JTextField();
					getContentPane().add(jTextFieldEsnCard, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jLabelCasco = new JLabel();
					getContentPane().add(jLabelCasco, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelCasco.setText("Casco *");
				}
				{
					jLabelCandado = new JLabel();
					getContentPane().add(jLabelCandado, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelCandado.setText("Candado *");
				}
				{
					jLabelChaleco = new JLabel();
					getContentPane().add(jLabelChaleco, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelChaleco.setText("Chaleco *");
				}
				{
					jLabelOtro = new JLabel();
					getContentPane().add(jLabelOtro, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelOtro.setText("Otro");
				}
				{
					jLabelEstadoEntrega = new JLabel();
					getContentPane().add(jLabelEstadoEntrega, new GridBagConstraints(0, 12, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelEstadoEntrega.setText("Estado entrega *");
				}
				{
					jTextAreaEstadoEntrega = new JTextArea();
					getContentPane().add(jTextAreaEstadoEntrega, new GridBagConstraints(2, 12, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jLabelTipoPago = new JLabel();
					getContentPane().add(jLabelTipoPago, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelTipoPago.setText("Tipo de pago *");
				}
				{
					ComboBoxModel jComboBoxTipoPagoModel = 
							new DefaultComboBoxModel(
									new String[] { "Tarjeta de crédito", "Banco", "Paypal" });
					jComboBoxTipoPago = new JComboBox();
					getContentPane().add(jComboBoxTipoPago, new GridBagConstraints(2, 10, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jComboBoxTipoPago.setModel(jComboBoxTipoPagoModel);
				}
				{
					jTextFieldPago = new JTextField();
					getContentPane().add(jTextFieldPago, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jLabelPago = new JLabel();
					getContentPane().add(jLabelPago, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelPago.setText("Pago *");
				}
				{
					jTextFieldOtro = new JTextField();
					getContentPane().add(jTextFieldOtro, new GridBagConstraints(2, 8, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaAlquiler = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaAlquiler, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaDevolucion = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaDevolucion, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaDevuelto = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaDevuelto, new GridBagConstraints(2, 11, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jButtonCancelar = new JButton();
					getContentPane().add(jButtonCancelar, new GridBagConstraints(2, 13, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonCancelar.setText("Cancelar");
					jButtonCancelar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonCancelar.setSize(70, 23);
					jButtonCancelar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonCancelarActionPerformed(evt);
						}
					});
				}
				{
					jButtonGuardar = new JButton();
					getContentPane().add(jButtonGuardar, new GridBagConstraints(2, 13, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonGuardar.setText("Guardar");
					jButtonGuardar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonGuardar.setSize(70, 23);
					jButtonGuardar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								jButtonGuardarActionPerformed(evt);
							} catch (DAOExcepcion e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
				{
					ComboBoxModel jComboBoxCascoModel = 
							new DefaultComboBoxModel(
									new String[] { "No", "Sí" });
					jComboBoxCasco = new JComboBox();
					getContentPane().add(jComboBoxCasco, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jComboBoxCasco.setModel(jComboBoxCascoModel);
				}
				{
					ComboBoxModel jComboBoxCandadoModel = 
							new DefaultComboBoxModel(
									new String[] { "No", "Sí" });
					jComboBoxCandado = new JComboBox();
					getContentPane().add(jComboBoxCandado, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jComboBoxCandado.setModel(jComboBoxCandadoModel);
				}
				{
					ComboBoxModel jComboBoxChalecoModel = 
							new DefaultComboBoxModel(
									new String[] { "No", "Sí" });
					jComboBoxChaleco = new JComboBox();
					getContentPane().add(jComboBoxChaleco, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jComboBoxChaleco.setModel(jComboBoxChalecoModel);
				}
				{
					jTextFieldIdBicicleta = new JTextField();
					getContentPane().add(jTextFieldIdBicicleta, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					ButtonGroup bgroup = new ButtonGroup();
				}
				{
					ButtonGroup bgroup = new ButtonGroup();
				}
				{
					ButtonGroup bgroup = new ButtonGroup();
				}

			}
			this.setSize(480, 480);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButtonCancelarActionPerformed(ActionEvent evt) {
		System.out.println("jButtonCancelar.actionPerformed, event="+evt);
		this.dispose();
	}
	
	private void jButtonGuardarActionPerformed(ActionEvent evt) throws DAOExcepcion {
		
		//TODO add your code for jButtonGuardar.actionPerformed
		Alquilerbicicleta alquilerBicicleta = new Alquilerbicicleta();
		AlquileresBicicleta gestionAlquileresBicicleta = new AlquileresBicicleta();
		
		Bicicletas gestionBicicleta = new Bicicletas();
		Bicicleta bicicleta = new Bicicleta();
		
		String emailConnectado = "thalia.voukena@upv.esnvlc.es";
		Usuario usuario = new Usuario();
		Usuarios gestionUsuarios = new Usuarios();
		Socio socio = new Socio();
		Socios gestionSocios = new Socios();
		AlquilerbicicletaId id = new AlquilerbicicletaId();
				
		System.out.println("jButtonGuardar.actionPerformed, event="+evt);

		if (gestionSocios.comprobarSocio(jTextFieldEsnCard.getText())) {
			socio = gestionSocios.recuperarSocio(jTextFieldEsnCard.getText());
			alquilerBicicleta.setSocio(socio);
			int idBici = -1;
			if (jTextFieldIdBicicleta.getText().length() != 0) {
				try {
					idBici= Integer.parseInt(jTextFieldIdBicicleta.getText());
				}
				catch(NumberFormatException nFE) {
					JOptionPane error = new JOptionPane();
					error.showMessageDialog(null, "ID deberia ser un numero", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			if (gestionBicicleta.comprobarBicicleta(idBici)){
				bicicleta = gestionBicicleta.recuperarBicicleta(idBici);
				id.setIdbicicleta(bicicleta.getIdbicicleta());
				id.setEsncard(jTextFieldEsnCard.getText());
				alquilerBicicleta.setFechaalquiler(jDateChooserFechaAlquiler.getDate());
				alquilerBicicleta.setFechadevolucion(jDateChooserFechaDevolucion.getDate());
				alquilerBicicleta.setCasco(jComboBoxCasco.getSelectedIndex());
				alquilerBicicleta.setCandado(jComboBoxCandado.getSelectedIndex());
				alquilerBicicleta.setChaleco(jComboBoxChaleco.getSelectedIndex());
				alquilerBicicleta.setOtro(jTextFieldOtro.getText());
				int pago = -1;
				if (jTextFieldPago.getText().length() != 0) {
					try {
						pago = Integer.parseInt(jTextFieldPago.getText());
					}
					catch(NumberFormatException nFE) {
						JOptionPane error = new JOptionPane();
						error.showMessageDialog(null, "pago deberia ser un numero", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
				alquilerBicicleta.setPago(pago);
				alquilerBicicleta.setTipopago(jComboBoxTipoPago.getSelectedIndex());
				alquilerBicicleta.setFechadevuelta(jDateChooserFechaDevuelto.getDate());
				alquilerBicicleta.setEstadoentrega(jTextAreaEstadoEntrega.getText());
				
				if(gestionAlquileresBicicleta.comprobarAlquilerBicicleta(alquilerBicicleta)) {
					if (gestionAlquileresBicicleta.anadirAlquilerBicicleta(alquilerBicicleta))
						this.dispose();
					else {
						JOptionPane error = new JOptionPane();
						error.showMessageDialog(null, "Fallo en guardado de datos", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			else {
				JOptionPane error = new JOptionPane();
				error.showMessageDialog(null, "No hay bicicleta con este id", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			JOptionPane error = new JOptionPane();
			error.showMessageDialog(null, "No hay socio con esta ESN Card", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		
	}
}

